<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>4、变位词组</title>
</head>

<body>
  <script>
    var groupAnagrams = function (strs) {
      let a, j, t = 0, r = [], m = new Map();
      for (let b of strs) {
        // step.1 排序后变位词会变成同一个词
        a = b.split('').sort().join('');
        // step.2 记录相同词index
        m.has(a) ? (j = m.get(a)) : m.set(a, j = t++);
        // step.3 分组存放
        (r[j] = r[j] || []).push(b);
      }
      return r;
    };
    console.log(groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]))
  </script>
</body>

</html>